{include file="/common/_meta"}
</head>
<body>
{include file="/common/top"}

<div class="main-container container-fluid">
    <div class="page-container">
        <!-- Page Sidebar -->
       {include file="/common/list"}
        <!-- Page Content -->
        <div class="page-content">

            <!-- Page Breadcrumb -->
            <div class="page-breadcrumbs">
                <ul class="breadcrumb">
                   <li>
                        <a href="#">系统</a>
                    </li>
                    <li>
                        <a href="">链接管理</a>
                    </li>
                    <li class="active">添加链接</li>
                </ul>
            </div>
            <!-- /Page Breadcrumb -->

            <!-- Page Body -->
            <div class="page-body">
                <a id="export" class="btn btn-sm btn-azure btn-addon" href="javascript:;" autocomplete="off">立即备份</a>
                <a id="optimize" href="{:url('data/optimize')}" class="btn btn-sm btn-azure btn-addon">优化表</a>
                <a id="repair" href="{:url('data/repair')}" class="btn btn-sm btn-azure btn-addon">修复表</a>
                <a  href="{:url('data/importlist')}" class="btn btn-sm btn-azure btn-addon">还原数据库</a>
                <div class="row">
                    <div class="col-lg-12 col-sm-12 col-xs-12">
                        <div class="widget">
                            <div class="widget-body">
                                <div class="flip-scroll">
                                    <form id="export-form" method="post" action="{:url('data/export')}">
                                    <table class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th style="width: 23px;" class="sorting_disabled">

                                                    <label style="font-weight: 300; ">
                                                        <input class="checkeds" checked="checked" type="checkbox" value="">
                                                        <span class="text"></span>
                                                    </label>

                                                </th>
                                                <th>表名</th>
                                                <th>数据量</th>
                                                <th>数据大小</th>
                                                <th>创建时间</th>
                                                <th>备份状态</th>
                                                <th>操作</th>
                                            </tr> 
                                        </thead>
                                        <tbody>
                                            {foreach name='list' item='table'}   
                                            <tr>            
                                                <td>
                                                    <label>
                                                    <input class="ids" checked="checked" type="checkbox" name="tables[]" value="{$table.name}"><span class="text"></span></label>
                                                </td>
                                                <td>{$table.name}</td>
                                                <td>{$table.rows}</td>
                                                <td>{$table.data_length|format_bytes}</td>
                                                <td>{$table.create_time}</td>
                                                <td class="info" style="background: none;">未备份</td>
                                                <td>
                                                    <a href="{:url('data/optimize',['tables'=>$table['name']])}">优化表</a>&nbsp;
                                                    <a href="{:url('data/repair',['tables'=>$table['name']])}">修复表</a>
                                                </td>
                                            </tr>
                                            {/foreach}
                                        </tbody>
                                    </table>
                                    </form>
                                </div>
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /Page Body -->
        </div>
        <!-- /Page Content -->

    </div>
</div>

    {include file="common/_footer"}
</body>
</html>

<script>
    layui.use(['jquery','layer'],function(){
        window.$ = layui.$;
        var layer = layui.layer;
        //备份表方法
        $("#export").click(function(){
            $(this).html("正在发送备份请求...");
            $.post(
                $("#export-form").attr("action"),
                $("#export-form").serialize(), 
                function(data){
               
                    if(data.code==1){
                        $("#export").html( "开始备份，请不要关闭本页面！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    }else{
                        layer.tips(data.msg, "#export", {
                            tips: [1, '#3595CC'],
                            time: 4000
                        });
                        $("#export").html("立即备份");
                    }
              
            }, "json");
            return false;  
        }); 

        //递归备份表
        function backup(tab,status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get( $("#export-form").attr("action"), tab, function(data){
                // console.log(data)
                if(data.code==1){
                    showmsg(tab, data.msg);

                    if(!$.isPlainObject(data.data.tab)){
                        $("#export").html("备份完成");
                        window.onbeforeunload = function(){ return null }
                        return;
                    } 

                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    $("#export").html("立即备份");
                }
            }, "json");
        }

        //修改备份状态
        function showmsg(tab, msg){
            $("table tbody tr").eq(tab.id).find(".info").html(msg)
        }

        //优化表
        $("#optimize").click(function(){
            $.post(this.href, $("#export-form").serialize(), function(data){
           
                layer.tips(data.msg, "#optimize", {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
    
            }, "json");
            return false;    
        });

        //修复表
        $("#repair").on("click",function(e){

            $.post(this.href, $("#export-form").serialize(), function(data){
                layer.tips(data.msg, "#repair", {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
            }, "json");
            return false; 
        });
    });

    $(".checkeds").click(function(){
        if ($(this).is(':checked')) {
            $('.ids').prop('checked','checked');
        } else{
            $('.ids').prop('checked',false);
        }
    });
</script>